package com.meitianhui.convenitenbuy.module.store.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding.view.RxView;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.store.model.StoreDetailMultiItemType;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.List;

import rx.functions.Action1;

import static com.meitianhui.convenitenbuy.Constants.BANNER_TIME;

/**
 * Created by Administrator on 2017/11/23.
 */

public class StoreDetailAdapter extends BaseMultiItemQuickAdapter<StoreDetailMultiItemType, BaseViewHolder> {

    private StoreDetailAdapterOnClickListener mOnClickListener;
    private RecyclerView rvGood = null;
    private StoreSelectGoodAdapter storeSelectGoodAdapter;
    private View mNotDataView;
    private int mPosition = 0;
    public StoreDetailAdapter(List<StoreDetailMultiItemType> data) {
        super(data);
        addItemType(StoreDetailMultiItemType.ITEM_BANNER, R.layout.item_store_detail_banner);
        addItemType(StoreDetailMultiItemType.ITEM_COUNT, R.layout.item_store_detail_count);
        addItemType(StoreDetailMultiItemType.ITEM_SERVICE, R.layout.item_store_detail_service);
        addItemType(StoreDetailMultiItemType.ITEM_TYPE, R.layout.item_store_detail_type);
        addItemType(StoreDetailMultiItemType.ITEM_GOOD, R.layout.item_store_detail_good);
        addItemType(StoreDetailMultiItemType.ITEM_NO_DATA, R.layout.data_empty_car);
    }

    @Override
    protected void convert(BaseViewHolder helper, StoreDetailMultiItemType item) {
        switch (helper.getItemViewType()) {
            case StoreDetailMultiItemType.ITEM_BANNER:   //banner布局
                Banner banner = helper.getView(R.id.banner);
                banner.setImages(item.mBannerData)
                        .setDelayTime(BANNER_TIME)   //设置轮播时间
                        .setImageLoader(new GlideImageLoader())
                        .start();
                break;
            case StoreDetailMultiItemType.ITEM_COUNT:  //COUNT
                helper.setText(R.id.tv_group_sale, "团购预售 " + (TextUtils.isEmpty(item.mGroupSale) ? "(0)" : "(" + item.mGroupSale + ")"));
                helper.setText(R.id.tv_favorable_price, "优惠活动 " + (TextUtils.isEmpty(item.mFavorablePrice) ? "(0)" : "(" + item.mFavorablePrice + ")"));
                helper.setText(R.id.tv_one_lottery, "一元抽奖 " + (TextUtils.isEmpty(item.mOneLottery) ? "(0)" : "(" + item.mOneLottery + ")"));
                RxView.clicks(helper.getView(R.id.tv_group_sale)).subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (mOnClickListener != null) {
                            mOnClickListener.groupSaleClick();
                        }
                    }
                });
                RxView.clicks(helper.getView(R.id.tv_favorable_price)).subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (mOnClickListener != null) {
                            mOnClickListener.favorablePriceClick();
                        }
                    }
                });
                RxView.clicks(helper.getView(R.id.tv_one_lottery)).subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (mOnClickListener != null) {
                            mOnClickListener.oneLotteryClick();
                        }
                    }
                });

                break;
            case StoreDetailMultiItemType.ITEM_SERVICE:  //服务
                helper.setText(R.id.tv_address, item.mStoreAddress);
                helper.setText(R.id.tv_desc, item.mDesc);
                RecyclerView rvService = helper.getView(R.id.rv_service);
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rvService.setLayoutManager(layoutManager);
                if (item.mStoreTypeList != null) {
                    if (item.mStoreTypeList.size() > 0) {
                        StoreServiceAdapter mServiceAdapter = new StoreServiceAdapter(item.mStoreTypeList);
                        rvService.setAdapter(mServiceAdapter);
                        rvService.setVisibility(View.VISIBLE);
                    } else {
                        rvService.setVisibility(View.GONE);
                    }
                }

                break;
            case StoreDetailMultiItemType.ITEM_TYPE:   //类型
                LinearLayout llAbout = helper.getView(R.id.ll_about);
                TextView tvPrice = helper.getView(R.id.tv_price);
                TextView tvTime = helper.getView(R.id.tv_time);
                TextView tvRange = helper.getView(R.id.tv_range);
                RecyclerView rvType = helper.getView(R.id.rv_type);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 6);
                gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
                rvType.setLayoutManager(gridLayoutManager);
                if (!TextUtils.isEmpty(item.mTime) && !"0".equals(item.mTime) && !"0.00".equals(item.mPrice) && !TextUtils.isEmpty(item.mPrice)) {
                    llAbout.setVisibility(View.VISIBLE);
                    if (TextUtils.isEmpty(item.mRange)) {
                        item.mRange = "0";
                    }
                    tvRange.setText(item.mRange + "米");
                    if (TextUtils.isEmpty(item.mTime)) {
                        item.mTime = "00:00-24:00";
                    }
                    tvTime.setText(item.mTime);
                    tvPrice.setText(item.mPrice + "元");
                }
                if (item.mTypeList.size() <= 0) {
                    rvType.setVisibility(View.GONE);
                }

                StoreSelectTypeAdapter mSelectTypeAdapter = new StoreSelectTypeAdapter(item.mTypeList);
                rvType.setAdapter(mSelectTypeAdapter);
                mSelectTypeAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (mOnClickListener != null) {
                            mPosition = position;
                            mSelectTypeAdapter.setCheckPosition(position);
                            mOnClickListener.typeItemClick(position, item.mTypeList.get(position));
                            mSelectTypeAdapter.notifyDataSetChanged();
                        }
                    }
                });
                mSelectTypeAdapter.setCheckPosition(mPosition);
                mSelectTypeAdapter.notifyDataSetChanged();
                break;

            case StoreDetailMultiItemType.ITEM_GOOD:
                rvGood = helper.getView(R.id.rv_detail_good_info);
                GridLayoutManager gridLayoutManager1;
                if(item.mStoreSelectedGood.getList().size()>0){
                  gridLayoutManager1 = new GridLayoutManager(mContext,3);
                }else{
                    gridLayoutManager1 = new GridLayoutManager(mContext,1);
                }
                gridLayoutManager1.setOrientation(GridLayoutManager.VERTICAL);
                rvGood.setLayoutManager(gridLayoutManager1);
                mNotDataView = LayoutInflater.from(mContext).inflate(R.layout.data_empty_car, (ViewGroup) rvGood.getParent(), false);
                if(storeSelectGoodAdapter==null){
                    storeSelectGoodAdapter = new StoreSelectGoodAdapter(item.mStoreSelectedGood.getList(),item.mStoreSelectedGood.getDoc_url());
                    rvGood.setAdapter(storeSelectGoodAdapter);
                }
                if(item.mStoreSelectedGood.getList().size()==0){
                    storeSelectGoodAdapter.setEmptyView(mNotDataView);
                }
                storeSelectGoodAdapter.setNewData(item.mStoreSelectedGood.getList(),item.mStoreSelectedGood.getDoc_url());
                break;
        }
    }


    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            GlideUtils.setRectImage(mContext,(String) path, R.mipmap.icon_default_picture, R.mipmap.icon_default_picture, imageView);
        }
    }


    public void setOnClickListener(StoreDetailAdapterOnClickListener l) {
        this.mOnClickListener = l;
    }

    public interface StoreDetailAdapterOnClickListener {
        //团购预售
        void groupSaleClick();

        //优惠抽奖
        void favorablePriceClick();

        //一元抽奖
        void oneLotteryClick();

        void typeItemClick(int position, String type);

    }
}
